package QuanLyNhaCungCap;

import KetNoiSQL.KetNoiSQL;
import KiemTra.KiemTra;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author PhuongNga
 */
public class DanhSachNhaCungCap extends javax.swing.JPanel {
    //Tạo 1 tableModel chứa dữ liệu cho bảng Nhà cung cấp (jTable)

    private DefaultTableModel modelNhaCungCap = new DefaultTableModel();
    String tenBang = "NhaCungCap";
    Vector vLay = new Vector();

    /**
     * Creates new form DanhSachNhaCungCap
     */
    public DanhSachNhaCungCap() {
        initComponents();

        try {
            vLay = NhaCungCap.xemNhaCungCap();
            this.taiBangNhaCungCap(tenBang, vLay);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

    }
    //Phương thức Tải dữ liệu vào bảng nhà cung cấp

    public void taiBangNhaCungCap(String tenBang, Vector vLay) throws SQLException {
        modelNhaCungCap.setRowCount(0);
        Vector cotNhaCungCap = new Vector();
        cotNhaCungCap.addElement("Mã");
        cotNhaCungCap.addElement("Tên nhà cung cấp");
        cotNhaCungCap.addElement("Tên đầy đủ");
        cotNhaCungCap.addElement("Địa chỉ");
        cotNhaCungCap.addElement("SDT");
        cotNhaCungCap.addElement("Fax");
        cotNhaCungCap.addElement("Email");
        cotNhaCungCap.addElement("Web");
        modelNhaCungCap.setColumnIdentifiers(cotNhaCungCap);

        NhaCungCap ncc = new NhaCungCap();
        for (int i = 0; i < vLay.size(); i++) {
            ncc = (NhaCungCap) vLay.get(i);

            Vector hangNhaCungCap = new Vector();
            hangNhaCungCap.addElement(ncc.getMaNhaCungCap());
            hangNhaCungCap.addElement(ncc.getTenNhaCungCap());
            hangNhaCungCap.addElement(ncc.getTenDayDu());
            hangNhaCungCap.addElement(ncc.getDiaChi());
            hangNhaCungCap.addElement(ncc.getSDT());
            hangNhaCungCap.addElement(ncc.getFax());
            hangNhaCungCap.addElement(ncc.getEmail());
            hangNhaCungCap.addElement(ncc.getWeb());
            modelNhaCungCap.addRow(hangNhaCungCap);

        }
        tbNhaCungCap.setModel(modelNhaCungCap);
    }

    //Xóa nhà cung cấp
    public void xoaNhaCungCap() {
        int show = JOptionPane.showConfirmDialog(this, "Bạn có đồng ý xóa dữ liệu nhà cung cấp ?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {
            int kt = 0, dem = 0;
            int[] rArr = tbNhaCungCap.getSelectedRows();
            try {
                for (int i = 0; i < rArr.length; i++) {
                    kt = NhaCungCap.xoaNhaCungCap(tbNhaCungCap.getValueAt(rArr[i], 1).toString());
                    if (kt == 1) {
                        dem++;
                    }
                }
                if (dem > 0) {
                    JOptionPane.showMessageDialog(this, "Xóa " + dem + " Nhà cung cấp", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                    vLay = NhaCungCap.xemNhaCungCap();
                    this.taiBangNhaCungCap(tenBang, vLay);
                } else {
                    JOptionPane.showMessageDialog(this, "Không xóa được!", "Thông báo", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }

    //Cập nhật nhà cung cấp

    public void capNhatNhaCungCap() {
        int kt = 0;
        int r = tbNhaCungCap.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn mục để cập nhật", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        NhaCungCap ncc = new NhaCungCap();

        int maNhaCungCap = Integer.parseInt(tbNhaCungCap.getValueAt(r, 0).toString().trim());
        ncc.setMaNhaCungCap(maNhaCungCap);
        ncc.setTenNhaCungCap(tbNhaCungCap.getValueAt(r, 1).toString().trim());
        ncc.setTenDayDu(tbNhaCungCap.getValueAt(r, 2).toString().trim());
        ncc.setDiaChi(tbNhaCungCap.getValueAt(r, 3).toString().trim());
        ncc.setSDT(tbNhaCungCap.getValueAt(r, 4).toString().trim());
        ncc.setFax(tbNhaCungCap.getValueAt(r, 5).toString().trim());
        ncc.setEmail(tbNhaCungCap.getValueAt(r, 6).toString().trim());
        ncc.setWeb(tbNhaCungCap.getValueAt(r, 7).toString().trim());

        try {
            kt = NhaCungCap.capNhatNhaCungCap(ncc);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Cập nhật thành công  !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                vLay = NhaCungCap.xemNhaCungCap();
                this.taiBangNhaCungCap(tenBang, vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Cập nhật bị lỗi.Xem lại", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //Kiểm tra tìm kiếm

    public boolean ktTimKiemNCC() {
        if (txtTimKiemNCC.getText().contains("'")) {
            JOptionPane.showMessageDialog(null, "Tên bạn nhập không chính xác.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            txtTimKiemNCC.setText("");
            txtTimKiemNCC.requestFocus();
            return false;
        } else if (txtTimKiemDC.getText().contains("'")) {
            JOptionPane.showMessageDialog(null, "Địa chỉ bạn nhập chưa chính xác.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            txtTimKiemDC.setText("");
            txtTimKiemDC.requestFocus();
            return false;
        }

        return true;
    }
    //Tìm kiếm nhà cung cấp

    public void timKiemNCC() throws SQLException {
        if (!this.ktTimKiemNCC()) {
            return;
        }
        Vector vlist = new Vector();
        if (txtTimKiemNCC.getText() != "" && txtTimKiemDC.getText().equals("")) {
            vlist.addElement(txtTimKiemNCC.getText().trim());
            vLay = NhaCungCap.layTheoTenNhaCungCap(vlist);
            this.taiBangNhaCungCap(tenBang, vLay);
        } else if (txtTimKiemNCC.getText().equals("") && txtTimKiemDC.getText() != "") {
            vlist.addElement(txtTimKiemDC.getText().trim());
            vLay = NhaCungCap.layTheoDiaChiNhaCungCap(vlist);
            this.taiBangNhaCungCap(tenBang, vLay);
        } else {
            vlist.addElement(txtTimKiemNCC.getText().trim());
            vlist.addElement(txtTimKiemDC.getText().trim());
            vLay = NhaCungCap.layTen_DiaChiNhaCungCap(vlist);
            this.taiBangNhaCungCap(tenBang, vLay);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbNhaCungCap = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btThem = new javax.swing.JButton();
        btCapNhat = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        btHuyBo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        txtTimKiemNCC = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtTimKiemDC = new javax.swing.JTextField();
        btTimKiem = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách nhà cung cấp", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(255, 0, 0)));

        tbNhaCungCap.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbNhaCungCap);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/add 1.png"))); // NOI18N
        btThem.setText("Thêm ");
        btThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemActionPerformed(evt);
            }
        });

        btCapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/update.png"))); // NOI18N
        btCapNhat.setText("Cập nhật");
        btCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatActionPerformed(evt);
            }
        });

        btXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/cross.png"))); // NOI18N
        btXoa.setText("Xóa");
        btXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaActionPerformed(evt);
            }
        });

        btHuyBo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Remove.png"))); // NOI18N
        btHuyBo.setText("Hủy bỏ");
        btHuyBo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btHuyBoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(btCapNhat)
                .addGap(35, 35, 35)
                .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addComponent(btHuyBo)
                .addGap(42, 42, 42))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btThem, btXoa});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btHuyBo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(24, 24, 24))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btCapNhat, btThem, btXoa});

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tìm kiếm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 255, 0)));

        jLabel9.setText("Nhập tên :");

        jLabel10.setText("Địa chỉ :");

        btTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Search.png"))); // NOI18N
        btTimKiem.setText("Tìm kiếm ");
        btTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTimKiemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtTimKiemNCC, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtTimKiemDC, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btTimKiem)
                    .addComponent(txtTimKiemDC, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTimKiemNCC, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(130, 130, 130)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 154, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
        // TODO add your handling code here:
        new ThemNhaCungCap().setVisible(true);
    }//GEN-LAST:event_btThemActionPerformed

    private void btCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatActionPerformed
        // TODO add your handling code here:
        this.capNhatNhaCungCap();
        //this.resetForm();
    }//GEN-LAST:event_btCapNhatActionPerformed

    private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
        // TODO add your handling code here:
        this.xoaNhaCungCap();
        // this.resetForm();
    }//GEN-LAST:event_btXoaActionPerformed

    private void btTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTimKiemActionPerformed
        try {
            this.timKiemNCC();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btTimKiemActionPerformed

    private void btHuyBoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btHuyBoActionPerformed
        txtTimKiemDC.setText("");
        txtTimKiemNCC.setText("");
        try {
            vLay = NhaCungCap.xemNhaCungCap();
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachNhaCungCap.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            this.taiBangNhaCungCap(tenBang, vLay);
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachNhaCungCap.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btHuyBoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhat;
    private javax.swing.JButton btHuyBo;
    private javax.swing.JButton btThem;
    private javax.swing.JButton btTimKiem;
    private javax.swing.JButton btXoa;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbNhaCungCap;
    private javax.swing.JTextField txtTimKiemDC;
    private javax.swing.JTextField txtTimKiemNCC;
    // End of variables declaration//GEN-END:variables
}
